var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var payUtil = require('../../../utils/pay.js');
Page({
  data: {
    statusType: ["全部", "待支付", "已支付", "退款中", "已完成"],
    currentTpye: 0,
    tabClass: ["", "", "", "", "", ""],
    orderList: [],
    type: "",
    status: "",
    pageSize: 10,
    pageNum: 1,
    noMore: true
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      type: options.type
    });
    // options['type'] = 'hotel';
    var title = "";
    var type = options.type;
    if (type == 'hotel') {
      title = "酒店订单"
    } else if (type == 'scene') {
      title = "鱼•订单"
    } else {
      title = '线路订单'
    };
    wx.setNavigationBarTitle({
      title: title,
    })


  },
  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentTpye = curType
    this.setData({
      currentTpye: curType
    });
    if (this.data.currentTpye == 0) {
      this.setData({
        status: ""
      });
    }
    if (this.data.currentTpye == 1) {
      // 待支付0
      this.setData({
        status: "0"
      });
    }
    if (this.data.currentTpye == 2) {
      // 已支付1
      this.setData({
        status: "1"
      });
    }
    if (this.data.currentTpye == 3) {
      // 退款中3
      this.setData({
        status: "3"
      });
    } if (this.data.currentTpye == 4) {
      // 已完成6
      this.setData({
        status: "6"
      });
    }
    this.getOrderList();
  },
  getOrderList() {
    let that = this;
    var data = {
      token: wx.getStorageSync("token"),
      type: that.data.type,
      status: that.data.status,
      pageSize: that.data.pageSize,
      pageNum: that.data.pageNum
    };
    that.setData({
      orderList: {}
    })


    util.request(api.OrderList, data).then(function (res) {
      if (res.code === "200") {
        console.log(res.datas);
        that.setData({
          orderList: res.datas
        });
      }
    });

  },
  payOrder(e) {
    var orderId = e.currentTarget.dataset.id;
    var orderType = this.data.type;
    var price = e.currentTarget.dataset.price;
    var needBill = e.currentTarget.dataset.needbill;
    // wx.redirectTo({
    //   url: '/pages/pay/ordersub?orderId='+orderId+"&orderType="+orderType+"&price="+price+"&needBill="+needBill,
    // })
    payUtil.pay(orderId, orderType)
  },
  applyBack(e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var url = api.ApplyPayBack;
    var index = parseInt(e.currentTarget.dataset.index);
    var data = {
      token: wx.getStorageSync("token"),
      orderId: orderId,
      type: that.data.type
    };
    var orderList = that.data.orderList;
    wx.showModal({
      title: '确认退款？',
      content: '',
      success(options) {
        if (options.confirm) {
          util.request(url, data).then(function (res) {
            if (res.code === "200") {
              //申请退款成功
              orderList[index].status = '3'
              that.setData({
                orderList: orderList
              })
            }
          });
        }
      }
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.getOrderList();
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  fnGoGoods: function (e) {
    var that = this;
    var goodsId = e.currentTarget.dataset.id;
    var type = that.data.type;
    var scenePath = "/pages/sceneTicketDetail/index?id=";
    var tourPath = "/pages/tourDetail/index?id=";
    var hotelPath = "/pages/hotelRoomDetail/index?id=";
    var url = "";
    if (type == "scene") {
      url = scenePath + goodsId;
    } else if (type == "tour") {
      url = tourPath + goodsId;
    } else {
      url = hotelPath + goodsId;
    }
    wx.navigateTo({
      url: url,
    })
  },
  fnShowDetail: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var url = "/pages/ucenter/orderDetail/index?id=" + id + "&type=" + that.data.type;
    wx.navigateTo({
      url: url,
    })
  }, fnGoGoods: function (e) {
    var that = this;
    var goodsId = e.currentTarget.dataset.id;
    var type = that.data.type;
    var scenePath = "/pages/sceneTicketDetail/scenticket?id=";
    var tourPath = "/pages/tourDetail/travelline?id=";
    var hotelPath = "/pages/hotelRoomDetail/roominfo?id=";
    var url = "";
    if (type == "scene") {
      url = scenePath + goodsId;
    } else if (type == "tour") {
      url = tourPath + goodsId;
    } else {
      url = hotelPath + goodsId;
    }
    wx.navigateTo({
      url: url,
    })
  }, fnDelOrder: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;

   var  reqData = { "id": id, "type": that.data.type}
    wx.showModal({
      title: '删除',
      content: '确认删除该订单吗？', success: function (options) {
        if (options.confirm) {
          util.request(api.DeleteOrder, reqData).then(function (res) {
            if (res.code === "200") {
              that.getOrderList()
            }
          });
        } else {

        }
      }
    })

  }
})